package J20241104_high_precision;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 高精度除法
 * User: 王圆豪
 * Date: 2024-11-05
 * Time: 21:21
 */
public class High_precision_division {
    public static void main(String[] args) {
        String s1 = "1234516";
        String s2 = "11";

        int[] arr1 = new int[s1.length()];
        int[] arr2 = new int[s2.length()];

        for (int i = 0; i < arr1.length; i++) {
            arr1[i] = s1.charAt(i) - '0';
        }
        for (int i = 0; i < arr2.length; i++) {
            arr2[i] = s2.charAt(i) - '0';
        }

        List<Integer> arr = div(arr1, arr2);

        System.out.println(arr.toString());
    }

    private static List<Integer> div(int[] arr1, int[] arr2) {
        List<Integer> arr = new ArrayList<>();
        int divisor = toInteger(arr2);
        int dividend = 0;
        for (int i = 0; i < arr1.length; i++) {
            dividend += arr1[i++];
            while (dividend < divisor && i < arr1.length){
                arr.add(0);
                dividend = dividend * 10 + arr1[i++];
            }
            if (i > arr1.length){
                break;
            }
            arr.add( dividend / divisor);
            dividend = ((dividend % divisor) * 10);
            i--;
        }
        int m = 0;
        while (true){
            if (arr.get(m) == 0){
                arr.remove(m);
                m++;
            }else break;
        }
        return arr;
    }

    private static int toInteger(int[] arr2) {
        int t = 0;
        for (int j : arr2) {
            t = t * 10 + j;
        }
        return t;
    }
}
